package com.jiashihui.web.controller;

import com.jiashihui.web.annotation.AccessLimit;
import com.jiashihui.web.config.Constant;
import com.jiashihui.web.exception.MyException;
import com.jiashihui.web.model.Product;
import com.jiashihui.web.model.User;
import com.jiashihui.web.model.base.BaseResponse;
import com.jiashihui.web.model.base.ResultObjectResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jiashihui
 * @describe: 测试
 * @date 2019/01/07
 */
@Controller
@RequestMapping("test")
public class HelloController {

    @AccessLimit(seconds = 2, maxCount = 2, needLogin = true)
    @GetMapping("/home1")
    @ResponseBody
    public String home1(ModelMap modelMap) {
        // @ModelAttribute：在Model上设置的值，对于所有被 @RequestMapping 注解的方法中，都可以通过 ModelMap 获取
        System.out.println(modelMap.get("author"));
        return modelMap.get("author").toString();
    }
    //或者 通过@ModelAttribute获取
    @GetMapping(value = {"/home2","/hi"})
    @ResponseBody
    public String home2(@ModelAttribute("author") String author) {
        System.out.println(author);
        return author + ":" + Constant.code;
    }

    @GetMapping("/home3/{ip:.+}")
    @ResponseBody
    public String home3(@PathVariable String ip) throws Exception {
        System.out.println("ip:" + ip);
        throw new MyException("101", "Sam 错误");
    }

    @GetMapping("/error")
    @ResponseBody
    public String error() throws Exception {
        return "error";
    }

    @GetMapping("/hello")
    public String hello(ModelMap map){
        List<User> userList = new ArrayList<>();
        User user = new User();
        user.setId("0012");
        user.setName("王二");
        user.setAge(28);
        userList.add(user);
        map.addAttribute("action", "add");
        map.addAttribute("userList",userList);
        return "index";
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable String id, ModelMap map){
        User user = new User();
        user.setId("0012");
        user.setName("王二");
        user.setAge(28);
        map.addAttribute("user",user);
        return "views/edit";
        // return "redirect:/users/";
    }

    @GetMapping("/getProduct/{id}")
    @ResponseBody
    public ResultObjectResponse getProduct(@PathVariable Long id){
        ResultObjectResponse response = new ResultObjectResponse();
        response.setCode("200");
        response.setMsg("操作成功！");
        Product product = new Product();
        return response;
    }

    @GetMapping("/testPath")
    @ResponseBody
    public BaseResponse testPath(HttpServletRequest request, HttpServletResponse response){
        BaseResponse result = new BaseResponse();
        result.setCode("200");
        result.setMsg("操作成功！");
//        (1)得到包含工程名的当前页面全路径：request.getRequestURI()
//        结果：/TEST/test.jsp
//                (2)得到工程名：request.getContextPath()
//        结果：/TEST
//                (3)得到当前页面所在目录下全名称：request.getServletPath()
//        结果：如果页面在jsp目录下 /TEST/jsp/test.jsp
//                (4)得到页面所在服务器的全路径：application.getRealPath("页面.jsp")
//        结果：D:/resin/webapps/TEST/test.jsp
//                (5)得到页面所在服务器的绝对路径：absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
//        结果：D:/resin/webapps/TEST
//        2.在类中取得路径：
//        (1)类的绝对路径：String u=Class.class.getClass().getResource("/").getPath()
//        结果：/D:/TEST/WebRoot/WEB-INF/classes/pack/
//                (2)得到工程的路径：System.getProperty("user.dir")
//        结果：D:/TEST
//        3.在Servlet中取得路径：
//        (1)得到工程目录：request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
//        结果：E:/Tomcat/webapps/TEST
//                (2)得到IE地址栏地址：request.getRequestURL()
//        结果：http://localhost:8080/TEST/test
//        (3)得到相对地址：request.getRequestURI()
//        结果：/TEST/test
        System.out.println("得到包含工程名的当前页面全路径:" + request.getRequestURI());
        System.out.println("得到工程名：" + request.getContextPath());
        System.out.println("得到当前页面所在目录下全名称:" + request.getServletPath());
        System.out.println("得到页面所在服务器的全路径：" + request.getRequestURI());
        System.out.println("得到IE地址栏地址：" + request.getRequestURL());
        return result;
    }

}
